<?php
	/**
	 *	Copyright (c) TeamFlamingo And gray 2004-2009
	 *	www.me-live.com.cn / www.i-gray.cn
	 *
	 *	[ WARNING ]
	 *		This is NOT a freeware!
	 *		You MUST get author's authorization before using it!
	 *	[ WARNING ]
	 *
	 *	Name : forum.php / Development Code Evolve
	 *	Created / Modify : 2010-2-15 / --
	 */

	if( !defined( 'IN_TEAME' ) )
	{
		exit( 'Access Denied' );
	}

	include	ME_CORE . '/template.h.php';
	include	ME_CORE . '/paging.h.php';

	$postsTopsArray	=	$forumTopsArray	=	$borderTopsArray	=	$postsArray	=	array();
	$postsTopsArrayKeys	=	null;

	$postsBgcolor	=	$postsForumBgcolor	=	'#efefef';


	//	[CH]	设置相关的Key并判断该Key是否存在
	$postsTopsArrayKeys	=	getMCHash( ME_POSTS_TOPS_RESULT_KEY );	//	[CH]	定义置顶文章的Memcached Key
	$postsTotalKeys		=	getMCHash( ME_POSTS_TOTAL_KEY );		//	[CH]	定义文章总数的Memcached Key

	//	[CH]	文章列表的Memcached Key
	//	[CH]	这里涉及到一个页码不同的问题
	$postsArrayKeys		=	getMCHash( ME_POSTS_RESULT_KEY . $MDK['p'] );

	//	[CH]	控制板块文章的显示
	$fid		=	isset( $MDK['f'] ) ? getMeInt( $MDK['f'] ) : false;
	$queryFid	=	$fid ? " AND `p`.`fid` = '{$fid}'" : ' ';

	//	[CH]	置顶文章
	if( $db->MC->get( $postsTopsArrayKeys ) )
	{
		$postsTopsArray	=	$db->MC->get( $postsTopsArrayKeys );
	}
	else
	{
		$postsTopsResult	=	$db->query( "SELECT `p`.`tid`, `p`.`title`, `p`.`lastReplyUid`, `p`.`timestamp`, `p`.`mid`, `p`.`read`, `p`.`reply`, `p`.`lastTimestamp`, `p`.`type`, `m`.`uid`, `m`.`author`, `u`.`username` as `replyUsername` FROM `mdk_posts` as `p`, `mdk_member` as `m`, `mdk_users` as `u` WHERE `type` != '0' AND `child` = '0' AND `p`.`uid` = `m`.`uid` AND `p`.`lastReplyUid` = `u`.`uid`" );

		while( $row = $db->assoc( $postsTopsResult ) )
		{
			$row['topstype']	= $row['type'] == 1 ? '社区置顶' : '板块置顶';
			$row['topsclass']	= $row['type'] == 1 ? 'forum_top' : 'board_top';
			$row['date']		= date( "Y-m-d", $row['timestamp'] );
			$row['lastTimestamp']	=	$row['lastTimestamp'] ? $row['lastTimestamp'] : $row['timestamp'];
			$row['lastReplyTime']	=	date( "Y-m-d h:i:s", $row['lastTimestamp'] );
			//	[CH]	获取最后回复人
			$row['replyUsername']	=	$row['reply'] == 0 && $row['lastReplyUid'] == $row['uid'] ? '' : $row['replyUsername'];

			if( $row['type'] == 1 )
			{
				$row['bgcolor']		= $postsForumBgcolor	= $postsForumBgcolor == '#efefef' ? '#fff' : '#efefef';
				$forumTopsArray[]	= $row;
			}
			else
			{
				$row['bgcolor']		= $postsBgcolor	= $postsBgcolor == '#efefef' ? '#fff' : '#efefef';
				$boardTopsArray[]	= $row;
			}
		}

		$postsTopsArray	=	array_merge( $forumTopsArray, $boardTopsArray );
		$db->MC->add( $postsTopsArrayKeys, $postsTopsArray );
	}

	//	[CH]	文章总数
	if( $db->MC->get( $postsTotalKeys ) )
		$postsTotal	=	$db->MC->get( $postsTotalKeys );
	else
	{
		$postsTotal	=	$db->assoc( $db->query( "SELECT COUNT(`id`) as `total` FROM `{$TablePre}posts` WHERE `type` = '0' AND `child` = '0'" ) );
		$db->MC->add( $postsTotalKeys, $postsTotal );
	}

	//	[CH]	调用分页函数
	$dblimit	=	sqlLimit( ME_MAXFORUMNUMBER );
	$page	=	parsePaging( $postsTotal['total'], $dblimit, ME_MAXFORUMNUMBER );

	//print_r( $dblimit );

	if( $db->MC->get( $postsArrayKeys ) )
		$postsArray	=	$db->MC->get( $postsArrayKeys );
	else
	{
		//	[CH]	用于设置隔行颜色
		$postsBgcolor		=	'#efefef';
		//	[CH]	用于确定广告位
		$currentTid			=	1;
		$postsResult	=	$db->query( "SELECT `p`.`tid`, `p`.`title`, `p`.`lastReplyUid`, `p`.`timestamp`, `p`.`mid`, `p`.`read`, `p`.`reply`, `p`.`lastTimestamp`, `m`.`uid`, `m`.`author`, `u`.`username` as `replyUsername` FROM `mdk_posts` as `p`, `mdk_member` as `m`, `mdk_users` as `u` WHERE `type` = '0' AND `child` = '0' AND `p`.`uid` = `m`.`uid` AND `p`.`lastReplyUid` = `u`.`uid` {$queryFid} ORDER BY `p`.`timestamp` DESC LIMIT {$dblimit[0]}, {$dblimit[1]}" );
		while( $row = $db->assoc( $postsResult ) )
		{
			$row['bgcolor']	= $postsBgcolor = $postsBgcolor == '#efefef' ? '#fff' : '#efefef';
			$row['date']	= date( "Y-m-d", $row['timestamp'] );
			$row['lastTimestamp']	=	$row['lastTimestamp'] ? $row['lastTimestamp'] : $row['timestamp'];
			$row['lastReplyTime']	=	date( "Y-m-d h:i:s", $row['lastTimestamp'] );
			$row['currentTid']		=	$currentTid;
			//	[CH]	获取最后回复人
			$row['replyUsername']	=	$row['reply'] == 0 && $row['lastReplyUid'] == $row['uid'] ? '' : $row['replyUsername'];
			$postsArray[]	=	$row;
			$currentTid++;
		}
		$db->MC->add( $postsArrayKeys, $postsArray );
	}

	$weAdsenseTid	=	ceil( count( $postsArray ) / 3 );

	$appRunTime	=	round( getCurrentTime() - $appStartTime, 4 );
?>